package client;

//~--- non-JDK imports --------------------------------------------------------
import SessionFactory.SessionFactoryUtil;
import dto.*;

//~--- JDK imports ------------------------------------------------------------
public class UserModel {

    JfMembersHome home;
    UserWarningHome warning;

    public UserModel() {
        home = new JfMembersHome();
        warning = new UserWarningHome();
    }

    public boolean addNewUser(JfMembers member) {
        System.out.println("i am in usermodel");
        boolean res = false;
        String password = member.getPassword();
        String hash = null;
        try {
            hash = UserModel.byteArrayToHexString(UserModel.computeHash(password));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("Hashed Password is " + hash);
        member.setPassword(hash);
        SessionFactoryUtil.startNew();
        home.persist(member);
        res = SessionFactoryUtil.commitTrans();
        SessionFactoryUtil.finish();
        return res;
    }
    //==========================================================================================

    public boolean editpw(JfMembers mem, String password, String cfpw, String npw) {
        boolean res = false;
        String typedoldpw = null;
        //=================1-Get Original Password==============================
        String originalpw = mem.getPassword();
        try {
            typedoldpw = UserModel.byteArrayToHexString(UserModel.computeHash(password));
        } catch (Exception ex) {
        }
        if (!originalpw.equals(typedoldpw)) {
            res = false;
            System.out.println("Not Match");
        } else {
            System.out.println("Matches");
            if (!npw.equals(cfpw)) {
                System.out.println("New Not Matches");
                res = false;
            } else {
                System.out.println("New Matches");
                try {
                    npw = UserModel.byteArrayToHexString(UserModel.computeHash(npw));
                } catch (Exception ex) {
                }
                SessionFactoryUtil.startNew();
                int updatepw = home.updatepw(npw, mem.getUsername());
                System.out.println(updatepw);
                SessionFactoryUtil.commitTrans();
                SessionFactoryUtil.finish();
                res = true;

            }
        }
        return res;
    }

    //=========================================== for login =====================================
    public JfMembers login(JfMembers member) {
        boolean found = false;
        SessionFactoryUtil.startNew();
        System.out.println("before call find by name");
        JfMembers memb = home.findByName(member);
        System.out.println("Obtained Password is " + memb.getPassword());
        SessionFactoryUtil.finish();

        return memb;
    }
    //====================================== //for login===========================================

    public boolean editprofile(JfMembers member) {
        boolean res = false;
        System.out.println("I am inside editProfile");
        System.out.println(member.getAvatar() + member.getCountry() + member.getEMail() + member.getFName() + member.getGender() + member.getLName() + member.getUsername());
        SessionFactoryUtil.startNew();
        int update = home.update(member);
        if (update == 1) {
            res = true;
            SessionFactoryUtil.commitTrans();
            SessionFactoryUtil.finish();
        } else {
            System.out.println("Can't Update");
        }
        return res;
    }
    //=============================================================================================

    //=============================================================================================
    public Announcement getAnnouncement() {
        Announcement announcement = new Announcement();

        return announcement;
    }

//========================Encryption Method=====================================
    public static byte[] computeHash(String x)
            throws Exception {
        java.security.MessageDigest d = null;
        d = java.security.MessageDigest.getInstance("SHA-1");
        d.reset();
        d.update(x.getBytes());
        return d.digest();
    }
//==============================================================================

    public static String byteArrayToHexString(byte[] b) {
        StringBuffer sb = new StringBuffer(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            int v = b[i] & 0xff;
            if (v < 16) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(v));
        }
        return sb.toString().toUpperCase();
    }
//==============================================================================
//~ Formatted by Jindent --- http://www.jindent.com
}